package com.tuantuan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tuantuan.dao.ClassrequireDao;
import com.tuantuan.dao.CommenDao;
import com.tuantuan.model.Classrequire;
import com.tuantuan.model.Courselist;
import com.tuantuan.model.CourselistWeeks;
import com.tuantuan.model.Courses;
import com.tuantuan.model.Teachers;
import com.tuantuan.service.CommenService;
import com.tuantuan.service.CourserListService;

/**
 * 课程表资料控制层
 * @author Administrator
 *
 */
@Controller
public class CourseListController {
	@Resource(name="CourseListDao")
	private CommenDao<Courselist> courselistDao;
	@Resource(name="CourseListService")
	private CommenService<Courselist> courselistSer;
	
	@Autowired
	private ClassrequireDao claRequire;
	
	@Resource(name="TeacherDao")
	private CommenDao<Teachers> teach;
	
	@Resource(name="CourseDao")
	private CommenDao<Courses> courDao;
	
	@Autowired
	private CourserListService courserlist;
	
	/**
	 * 添加课程表信息操作
	 * @param request
	 * @param clw
	 */
	@RequestMapping("/courselist/addCourseList.do")
	@ResponseBody
	public String addCouseList(HttpServletRequest request,CourselistWeeks clw){
		return courselistSer.addEntity(clw);
	}
	
	/**
	 * 课程表信息添加页面初始化
	 * @param request
	 * @param c
	 * @return
	 */
	@RequestMapping("/courselist/tosAddCourseList.do")
	public String tosAddCourseList(HttpServletRequest request,Classrequire cr,Teachers tc,Courses cs){
		List<Classrequire> crlist = claRequire.queryAllClassrequire(cr);
		List<Courses> co = courDao.queryAll(cs);
		List<Teachers> tcs = teach.queryAll(tc);
		
		//班级资料
		request.setAttribute("classAll", crlist);
		//课程资料
		request.setAttribute("courseAll", co);
		//教师资料
		request.setAttribute("teacherAll", tcs);
		
		return "courselist/addCourseList";
				
	}
	/**
	 * 课程表数据分页查询
	 * @param request
	 * @param response
	 * @param cl
	 * @return map（select结果集）
	 */
	@RequestMapping("/courselist/queryCourselist.do")
	@ResponseBody
	public Map<String,Object> queryByPage(HttpServletRequest request,HttpServletResponse response,Courselist cl){
		Map<String,Object> map=new HashMap<String, Object>();
		Courselist c = courselistDao.queryByPage(cl);
		map.put("page",c);
		return map;

	}
	/**
	 * 获取课程表数据链接（准备对应数据）
	 * @param request
	 * @param cr
	 * @param tc
	 * @param cs
	 * @return
	 */
	@RequestMapping("/courselist/toQueryCourselist.do")
	public String toQueryCourselist(HttpServletRequest request,Classrequire cr,Teachers tc,Courses cs){
		List<Classrequire> crlist = claRequire.queryAllClassrequire(cr);
		List<Courses> co = courDao.queryAll(cs);
		List<Teachers> tcs = teach.queryAll(tc);
		
		//班级资料
		request.setAttribute("classAll", crlist);
		//课程资料
		request.setAttribute("courseAll", co);
		//教师资料
		request.setAttribute("teacherAll", tcs);
		return "courselist/courselist";
	}
	/**
	 * 删除课程表资料
	 */
	@RequestMapping("/courselist/deleteCourselist.do")
	public String deleteStudent(HttpServletRequest request,int clId,int pageCounts){
		List<Classrequire> crlist = claRequire.queryAllClassrequire(new Classrequire());
		List<Courses> co = courDao.queryAll(new Courses());
		List<Teachers> tcs = teach.queryAll(new Teachers());
		String flag = courselistSer.deleteEntity(clId);
		//班级资料
		request.setAttribute("classAll", crlist);
		//课程资料
		request.setAttribute("courseAll", co);
		//教师资料
		request.setAttribute("teacherAll", tcs);
		request.setAttribute("pageCounts", pageCounts);
		request.setAttribute("flag", flag);
		return "courselist/courselist";
		
	}
	
	/**
	 * 课程表信息更新界面加载
	 * @param request
	 * @param stuId 学生主键ID
	 * @return
	 */
	@RequestMapping("/courselist/toUpdateCourselist.do")
	private String toUpdateCourselist(HttpServletRequest request, int clId){
		List<Classrequire> crlist = claRequire.queryAllClassrequire(new Classrequire());
		List<Courses> co = courDao.queryAll(new Courses());
		List<Teachers> tcs = teach.queryAll(new Teachers());
		request.setAttribute("classAll", crlist);
		//课程资料
		request.setAttribute("courseAll", co);
		//教师资料
		request.setAttribute("teacherAll", tcs);
		Courselist c = new Courselist();
		c.setClId(clId);
		Courselist cl = courselistDao.queryById(c);
		CourselistWeeks cw = new CourselistWeeks();
		cw.setClId(cl.getClId());
		cw.setCoId(cl.getClId());
		cw.setTcId(cl.getTcId());
		cw.setClassId(cl.getClassId());
		cw.setPlace(cl.getPlace());
		cw.setBeginClass(cl.getBeginClass());
		cw.setEndClass(cl.getEndClass());
		cw.setDays(cl.getDays());
		cw.setWeeks(cl.getWeeks());
		String str[] = cw.getWeeks().split(",");
		for(String s : str){
			if("1".equals(s)){
				cw.setWeek1(s);
			}
			else if("2".equals(s)){
				cw.setWeek2(s);
			}
			else if("3".equals(s)){
				cw.setWeek3(s);
			}
			else if("4".equals(s)){
				cw.setWeek4(s);
			}
			else if("5".equals(s)){
				cw.setWeek5(s);
			}
			else if("6".equals(s)){
				cw.setWeek6(s);
			}
			else if("7".equals(s)){
				cw.setWeek7(s);
			}
			else if("8".equals(s)){
				cw.setWeek8(s);
			}
			else if("9".equals(s)){
				cw.setWeek9(s);
			}
			else if("10".equals(s)){
				cw.setWeek10(s);
			}
			else if("11".equals(s)){
				cw.setWeek11(s);
			}
			else if("12".equals(s)){
				cw.setWeek12(s);
			}
			else if("13".equals(s)){
				cw.setWeek13(s);
			}
			else if("14".equals(s)){
				cw.setWeek14(s);
			}
			else if("15".equals(s)){
				cw.setWeek15(s);
			}
			else if("16".equals(s)){
				cw.setWeek16(s);
			}
			else if("17".equals(s)){
				cw.setWeek17(s);
			}
			else if("18".equals(s)){
				cw.setWeek18(s);
			}
			else if("19".equals(s)){
				cw.setWeek19(s);
			}
			else if("20".equals(s)){
				cw.setWeek20(s);
			}
			else if("21".equals(s)){
				cw.setWeek21(s);
			}
		}
		request.setAttribute("courselistWeeks", cw);
		
		return "courselist/courseListView";
	}
	/**
	 * 更新课程表资料
	 * @param request
	 * @param response
	 * @param c
	 * @return
	 */
	@RequestMapping("/courselist/updateCourseList.do")
	@ResponseBody
	public String  updateStudent(HttpServletRequest request,HttpServletResponse response,CourselistWeeks c){
		
		return courselistSer.updateEntity(c);
	}

	//对接手机端
	@RequestMapping("/courselist/queryCourseList.do")
	@ResponseBody
	public String queryCourseList(HttpServletRequest request,HttpServletResponse response,String week){
		return courserlist.queryCourseList(week);
	}
	
	
	
	public CommenDao<Courselist> getCourselistDao() {
		return courselistDao;
	}

	public void setCourselistDao(CommenDao<Courselist> courselistDao) {
		this.courselistDao = courselistDao;
	}

	public CommenService<Courselist> getCourselistSer() {
		return courselistSer;
	}

	public void setCourselistSer(CommenService<Courselist> courselistSer) {
		this.courselistSer = courselistSer;
	}

	public CommenDao<Teachers> getTeach() {
		return teach;
	}

	public void setTeach(CommenDao<Teachers> teach) {
		this.teach = teach;
	}

	public CommenDao<Courses> getCourDao() {
		return courDao;
	}

	public void setCourDao(CommenDao<Courses> courDao) {
		this.courDao = courDao;
	}
	
	

	

}
